//
//  FKCommitConsumeVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/5/21.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKCommitConsumeVC.h"
#import "FKOrderGoods.h"
#import "FKConsumeRuleView.h"

@interface FKCommitConsumeVC ()

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIView *header;

@property (nonatomic, weak) UIImageView *pictureView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *subTitleLabel;
@property (nonatomic, weak) UILabel *numLabel;

// 日期和数量
@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) UILabel *num;

@property (nonatomic, weak) UITextField *nameField;
@property (nonatomic, weak) UITextField *mobileField;
@property (nonatomic, weak) UITextView *ruleView;

// 确认
@property (nonatomic, strong) UIButton *isConfirm;

@property (nonatomic, weak) UIButton *confirmBtn;

@end

@implementation FKCommitConsumeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"核销信息";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    UIView *header = [[UIView alloc] init];
    self.header = header;
    header.backgroundColor = [UIColor whiteColor];
    
    // 商品view
    UIView *goodsView = [[UIView alloc] init];
    [header addSubview:goodsView];
    
    UIImageView *pictureView = [[UIImageView alloc] init];
    [goodsView addSubview:pictureView];
    self.pictureView = pictureView;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [goodsView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    titleLabel.numberOfLines = 2;
    
    UILabel *subTitleLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
    [goodsView addSubview:subTitleLabel];
    self.subTitleLabel = subTitleLabel;
    
    UIView *separator = [[UIView alloc] init];
    [goodsView addSubview:separator];
    separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    [goodsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(fk_adjustW(112));
    }];
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(78));
        make.centerY.offset(0);
        make.left.offset(12);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pictureView.mas_right).offset(8);
        make.right.offset(-12);
        make.top.equalTo(pictureView).offset(4);
    }];
    [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.right.offset(-30);
        make.bottom.equalTo(pictureView).offset(-4);
    }];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.bottom.offset(0);
        make.height.offset(0.8);
    }];
//    UILabel *numLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
//    [goodsView addSubview:numLabel];
//    self.numLabel = numLabel;
    
    // 日期和数量
    self.time = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [header addSubview:self.time];
    self.time.text = [NSString stringWithFormat:@"预约日期:%@",self.appDate];
    self.num = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [header addSubview:self.num];
    self.num.text = @"预约数量:0";
    UIView *separator2 = [[UIView alloc] init];
    [header addSubview:separator2];
    separator2.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.top.mas_equalTo(goodsView.mas_bottom).offset(10);
        make.height.offset(24);
    }];
    [separator2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.top.mas_equalTo(self.time.mas_bottom).offset(10);
        make.height.offset(1);
    }];
    [self.num mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.top.mas_equalTo(separator2.mas_bottom).offset(10);
        make.height.offset(24);
    }];
    // 填写信息
    UIView *grayView = [[UIView alloc] init];
    grayView.backgroundColor = [UIColor fk_colorWithHexString:@"#f5f5f5"];
    [header addSubview:grayView];
    
    [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.right.offset(0);
        make.top.mas_equalTo(self.num.mas_bottom).offset(10);
    }];
    
    UILabel *contactTitle = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor999999 text:@"请填写你的联系方式:"];
    [grayView addSubview:contactTitle];
    [contactTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(24);
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(12);
    }];
    
    UILabel *nameLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"姓名:"];
    [header addSubview:nameLabel];
    
    UITextField *nameField = [[UITextField alloc] init];
    [header addSubview:nameField];
    nameField.placeholder = @"请输入姓名";
    self.nameField = nameField;
    nameField.font = fk_adjustFont(14);
    nameField.borderStyle = UITextBorderStyleNone;
    
    UIView *separator3 = [[UIView alloc] init];
    [header addSubview:separator3];
    separator3.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *mobileLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"手机号:"];
    [header addSubview:mobileLabel];
    
    UITextField *mobileField = [[UITextField alloc] init];
    [header addSubview:mobileField];
    mobileField.placeholder = @"请输入手机号码";
    self.mobileField = mobileField;
    mobileField.font = fk_adjustFont(14);
    mobileField.borderStyle = UITextBorderStyleNone;
    mobileField.keyboardType = UIKeyboardTypeNumberPad;
    
   
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contactTitle);
        make.height.offset(24);
        make.top.equalTo(grayView.mas_bottom).offset(10);
    }];
    [nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(nameLabel.mas_right).offset(10);
        make.height.offset(fk_adjustW(28));
        make.right.mas_equalTo(-12);
        make.centerY.equalTo(nameLabel);
    }];
    
    [separator3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.top.mas_equalTo(nameLabel.mas_bottom).offset(10);
        make.height.offset(1);
    }];
    [mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.left.equalTo(nameLabel);
        make.top.equalTo(separator3.mas_bottom).offset(10);
    }];
    [mobileField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(mobileLabel.mas_right).offset(10);
        make.height.offset(fk_adjustW(28));
        make.right.mas_equalTo(-12);
        make.centerY.equalTo(mobileLabel);
    }];
    
    
    UIView *grayView2 = [[UIView alloc] init];
    grayView2.backgroundColor = [UIColor fk_colorWithHexString:@"#f5f5f5"];
    [header addSubview:grayView2];
    
    [grayView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(10);
        make.left.right.offset(0);
        make.top.mas_equalTo(mobileLabel.mas_bottom).offset(10);
    }];
    
    FKConsumeRuleView *ruleTopView = [[NSBundle mainBundle] loadNibNamed:@"FKConsumeRuleView" owner:self options:nil].lastObject;
    [header addSubview:ruleTopView];
    [ruleTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44);
        make.top.mas_equalTo(grayView2.mas_bottom);
        make.left.right.mas_equalTo(0);
    }];
    // rule
    UITextView *ruleView = [[UITextView alloc] init];
    [header addSubview:ruleView];
    self.ruleView = ruleView;
    ruleView.editable = false;
    ruleView.font = fk_adjustFont(14);
    ruleView.textColor = fkColor999999;
    [ruleView fk_viewCornerRadius:5 borderWidth:0.8 borderColor:[UIColor fk_colorWithHexString:@"#ff2e2e"]];
    
    [ruleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.top.equalTo(ruleTopView.mas_bottom).offset(0);
//        make.bottom.offset(0);
//        make.bottom.offset(-30);
        make.height.offset(0);
    }];
    
    self.isConfirm = [UIButton fk_btnWithFont:fkFont12 title:@"我同意提交预约后不可更改或取消"];
    [self.isConfirm setTitleColor:fkColor666666 forState:UIControlStateNormal];
//    self.isConfirm.titleLabel.textColor = fkColor666666;
    [self.isConfirm setImage:[UIImage imageNamed:@"cs_uncheck"] forState:UIControlStateNormal];
    [self.isConfirm setImage:[UIImage imageNamed:@"cs_check"] forState:UIControlStateSelected];
    [self.isConfirm addTarget:self action:@selector(confirmChange:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:self.isConfirm];
    [self.isConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.height.offset(22);
        make.bottom.mas_equalTo(-10);
    }];
    
    header.frame = CGRectMake(0, 0, fkScreenW, fk_adjustW(112) + 264 + 50 + 20);
    self.tableView.tableHeaderView = header;
    
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fk_adjustW(120))];
    footer.backgroundColor = [UIColor fk_colorWithHexString:@"#f5f5f5"];
    
    UIButton *confirmBtn = [UIButton fk_btnWithTarget:self action:@selector(confirmBtnClicked) font:fk_adjustFont(18) normalTitle:@"确认预约" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
    [footer addSubview:confirmBtn];
    self.confirmBtn = confirmBtn;
    [confirmBtn fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.height.offset(fk_adjustW(44));
        make.top.offset(20);
    }];
    self.tableView.tableFooterView = footer;
    // 设置数据
    [pictureView sd_setImageWithURL:[NSURL URLWithString:self.goods.good_img] placeholderImage:kPlaceholder_1x1];
    titleLabel.text = self.goods.good_title;
    subTitleLabel.text = self.goods.option_title;
    
    [self loadData];
}

- (void)confirmChange:(UIButton *)btn {
    btn.selected = !btn.selected;
}

- (void)confirmBtnClicked
{
    [self.view endEditing:true];
    
    if (self.nameField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入姓名"];
        return;
    }
    if (self.mobileField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入手机号"];
        return;
    }
    if (self.mobileField.text.length != 11) {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号"];
        return;
    }
    if (!self.isConfirm.isSelected) {
        [SVProgressHUD showInfoWithStatus:@"请同意提交预约后不可更改或取消"];
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"order_id"] = self.goods.order_id;
    param[@"order_goods_id"] = self.goods.id;
    param[@"reserve_time"] = self.reserve_time;
    param[@"reserve_nums"] = self.goods.num;
    param[@"full_name"] = self.nameField.text;
    param[@"full_mobile"] = self.mobileField.text;
    [FKHttpTool postWithSuffix:@"m=Home&c=OrderList&a=reserve" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"预约成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOrderListVC" object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOrderDetailVC" object:nil];
            fkPopVC;
        }
    } failure:nil];
}

#pragma mark - 网络请求
- (void)loadData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"order_goods_id"] = self.goods.id;
    [FKHttpTool postWithSuffix:@"m=Home&c=OrderList&a=ReservationInformation" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSDictionary *rules = result.data[@"rules"];
            self.num.text = [NSString stringWithFormat:@"预约数量:%@",result.data[@"list"][@"num"]];
            NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:rules[@"title"]];
            NSAttributedString *content = [[NSAttributedString alloc] initWithData:[rules[@"info"] dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentOption:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
            [title appendAttributedString:content];
            self.ruleView.attributedText = title;
            
            
//            self.ruleView.text = fkFORMAT(@"%@:%@", rules[@"title"], rules[@"info"]);
            CGSize ruleSize = [self.ruleView sizeThatFits:CGSizeMake(fkScreenW - 24 - 16, MAXFLOAT)];
            CGFloat ruleH = ruleSize.height + 30;
            [self.ruleView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(ruleH);
            }];
//            CGRect ruleFrame = [self.ruleView.text boundingRectWithSize:CGSizeMake(fkScreenW - 24 - 16, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.ruleView.font} context:nil];
//            CGFloat ruleH = ruleFrame.size.height + 16;
            self.header.fk_height = fk_adjustW(112) + 264 + 50 + ruleH;
            self.tableView.tableHeaderView = self.header;
        }
    } failure:^(NSError *error) {
        
    }];
}


@end
